//
//  SecondViewController.m
//  百度地图
//
//  Created by ljm on 2017/7/6.
//  Copyright © 2017年 ljm. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()<BMKPoiSearchDelegate>

@property (nonatomic,strong) BMKMapView *myMapView;

@property (nonatomic,strong) BMKPoiSearch *myMapPoiSearch;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadMapView];
}

#pragma mark -- 加载地图
- (void) loadMapView
{
    self.title = @"BMKMap";
    _myMapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight - 64)];
    _myMapView.mapType = BMKMapTypeStandard;
    [_myMapView setTrafficEnabled:YES];
    self.view = _myMapView;
    

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(screenWidth - 80, screenHeight - 40, 80, 40)];
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"search" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(searchDetailPlace) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}


- (void)searchDetailPlace
{
    _myMapPoiSearch = [[BMKPoiSearch alloc] init];
    _myMapPoiSearch.delegate = self;
    
    
    BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc] init];
    option.location = CLLocationCoordinate2DMake(39.915, 116.404);
    option.keyword = @"小吃";
    BOOL flag = [_myMapPoiSearch poiSearchNearBy:option];
    
    if (flag) {
        NSLog(@"周边检索成功");
    }else{
        NSLog(@"周边检索失败");
    }
    
}

- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode
{
    if (errorCode == BMK_SEARCH_NO_ERROR) {
        NSLog(@"%@",poiResult);
    }else if (errorCode == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        NSLog(@"起点有歧义");
    }else{
        NSLog(@"未找到结果");
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    _myMapPoiSearch.delegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
